
namespace KlearTweet.Client.Search
{
    public class SearchItem
    {
        public const string FriendsTimeline = "friends";
        public const string UserTimeline = "user";

        public string DisplayName { get; set; }

        public string SearchText { get; set; }

        public string DisplayText
        {
            get
            {
                if (string.IsNullOrEmpty(DisplayName))
                    return SearchText;

                return SearchText + " (" + DisplayName + ")";
            }
        }

        public System.Windows.Controls.Border Icon
        {
            get
            {
                return new System.Windows.Controls.Border { Background = ForegroundColor };
            }
        }

        public System.Windows.Media.Brush ForegroundColor { get; set; }


        public override string ToString()
        {
            return SearchText;
        }

        public string GetFullText()
        {
            return SearchText + "\n" + DisplayName;
        }


        public static SearchItem FromHome()
        {
            return new SearchItem
            {
                DisplayName = "Home",
                SearchText = "",
                ForegroundColor = new System.Windows.Media.SolidColorBrush(
                    new System.Windows.Media.Color { A = 255, R = 0, G = 0, B = 0 }),
            };
        }

        public static SearchItem FromHistoryPoint(string history)
        {
            return new SearchItem
            {
                DisplayName = "",
                SearchText = history,
                ForegroundColor = new System.Windows.Media.SolidColorBrush(
                    new System.Windows.Media.Color { A = 255, R = 0, G = 0, B = 100 }),
            };
        }

        public static SearchItem From(Model.Account account, string resource)
        {
            return new SearchItem
            {
                DisplayName = GetDisplayName(account, resource),
                SearchText = "@" + account.User.UserName + ":" + resource,
                ForegroundColor = new System.Windows.Media.SolidColorBrush(
                    new System.Windows.Media.Color { A = 255, R = 0, G = 100, B = 0 }),
            };
        }

        private static string GetDisplayName(Model.Account account, string resource)
        {
            switch (resource)
            {
                case FriendsTimeline:
                    return "My Friends Tweets";
                case UserTimeline:
                    return "My Tweets";
            }
            throw new System.Exception("Unknown: " + resource);
        }
    }
}